package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.SettingsService;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserService;
import com.tanhua.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * @author Administrator
 */
@RestController
@RequestMapping(path = "/users")
public class UsersController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private SettingsService settingsService;
    @Autowired
    private UserService userService;
    /**
     * 查询用户基础信息
     *
     * @param userID
     * @param token
     * @return
     */
    @GetMapping
    public ResponseEntity userInfo(Long userID, @RequestHeader("Authorization") String token) {
        //1. 校验token
        boolean verifyToken = JwtUtils.verifyToken(token);
        if(!verifyToken) {
            return ResponseEntity.status(401).body(null);
        }
        //2. 获取token中的用户信息
        Claims claims = JwtUtils.getClaims(token);
        String id = claims.get("id").toString();

        if (userID == null) {
            userID = Long.valueOf(id);
        }

        UserInfoVo userInfo = userInfoService.findById(userID);

        return ResponseEntity.ok(userInfo);
    }

    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo,@RequestHeader("Authorization") String token) {
        //1、判断token是否合法
        boolean verifyToken = JwtUtils.verifyToken(token);
        if(!verifyToken) {
            return ResponseEntity.status(401).body(null);
        }
        //2、获取id
        Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer) claims.get("id");

        //设置id
        userInfo.setId(Long.valueOf(id));
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }
    @GetMapping("/settings")
    public ResponseEntity settings(){
        SettingsVo settingsVo = settingsService.settingsVo();
        return ResponseEntity.ok(settingsVo);
    }
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map){
        String content = (String) map.get("content");
        settingsService.savatext(content);
        return ResponseEntity.ok(null);
    }

    @PostMapping("/notifications/setting")
    public ResponseEntity notifications(@RequestBody Map map){
        settingsService.savanotifications(map);
        return ResponseEntity.ok(null);
    }
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {

        PageResult pr = settingsService.blacklist(page,size);

        return ResponseEntity.ok(pr);
    }
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity deleteBlackList(@PathVariable("uid") Long blackUserId) {
        settingsService.deleteBlackList(blackUserId);
        return ResponseEntity.ok(null);
    }
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(){
        String mobile = UserHolder.getMobile();
        userService.sendCode(mobile);
        return ResponseEntity.ok(null);
    }

    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(String verificationCode){

        boolean verification = userService.checkVerificationCode(verificationCode);
        return ResponseEntity.ok(verification);
    }
    @PostMapping("/phone")
    public ResponseEntity updataphone(@RequestBody Map map){
        String phone = (String) map.get("phone");
        userService.updataphone(phone);
        return ResponseEntity.ok(null);
    }

    @PostMapping("/header")
    public ResponseEntity header(MultipartFile headPhoto) throws IOException {
        // String headPhoto = (String) map.get("headPhoto");
        Long id = UserHolder.getUserId();
        userInfoService.updateHead(headPhoto,id);
        return ResponseEntity.ok(null);
    }

}